﻿using Microsoft.Extensions.DependencyInjection;

namespace ConsoleApp.TestFunc.测试代码
{

    public static class DITest7
    {

        public static void Run()
        {
            /*List<Student> students = new List<Student>
        {
            new Student { Name = "Alice", Courses = new List<string> { "Math", "Physics" } },
            new Student { Name = "Bob", Courses = new List<string> { "Chemistry", "Biology" } }
        };

        // 使用 SelectMany 展平所有课程
        var allCourses = students
            .SelectMany(s => s.Courses)
            .ToList();

        Console.WriteLine("所有课程：");
        foreach (var course in allCourses)
        {
            Console.WriteLine(course);
        }

        // 使用 SelectMany 关联学生和课程
        var studentCourses = students
            .SelectMany(
                s => s.Courses,
                (s, course) => new { s.Name, course }
            )
            .ToList();

        Console.WriteLine("\n学生及其课程：");
        foreach (var item in studentCourses)
        {
            Console.WriteLine($"{item.Name} - {item.course}");
        }*/
            List<Department> departments = new List<Department>
            {
                new Department { Id = 1, Name = "HR" },
                new Department { Id = 2, Name = "IT" },
                new Department { Id = 3, Name = "Finance" }
            };

            List<Employee> employees = new List<Employee>
            {
                new Employee { Id = 101, Name = "Alice", DepartmentId = 1 },
                new Employee { Id = 102, Name = "Bob", DepartmentId = 2 },
                new Employee { Id = 103, Name = "Charlie", DepartmentId = 2 }
            };

            var result = departments.GroupJoin(
                employees,
                d => d.Id,
                e => e.DepartmentId,
                (d, eGroup) => new
                {
                    Department = d.Name,
                    Employees = eGroup.ToList()
                }
            ).ToList();

            foreach (var item in result)
            {
                Console.WriteLine($"Department: {item.Department}");
                foreach (var emp in item.Employees)
                {
                    Console.WriteLine($"  - {emp.Name}");
                }
            }
        }
    }
}
